package jdaap.gui.util;

import java.lang.reflect.Field;

public class BeansUtil {

	
	public static Object getValueAt(Object objeto, String attributo) 
	{
    	if(objeto == null || attributo == null)
			return null;
		
		Object valor = getValueField(getObjectField(objeto, attributo), getNameField(attributo));
    	
		return valor;
	}
	
	public static Object getValueAtEspecial(Object objeto, String attributo) 
	{
    	if(objeto == null || attributo == null)
			return null;
    	
    	Object valor = null; 
    	
    	if(attributo.contains("#")){
    		
    		valor = "";
    		Object aux = null;
    		String[] values = attributo.split("#");
    		for (int i = 0; i < values.length; i++) {
				String var = values[i];
				
				aux = getValueField(getObjectField(objeto, var), getNameField(var));
				if(aux != null){
					valor = valor.toString()+aux;
				}else{
					valor = valor.toString()+var;
				}
			}
    	}else{
    		valor = getValueField(getObjectField(objeto, attributo), getNameField(attributo));
    	}
		return valor;
	}
	
	public static Object setValueAtEspecial(Object objeto, Object value, String attributo) 
	{

		if(attributo.contains("#")){
			String att = attributo.substring(0, attributo.indexOf("#"));
			return setValueAtEspecial(objeto, value, att);
		}

		return setValueAt(objeto, value, attributo);

	}
	
	public static Object setValueAt(Object objeto, Object value, String attributo) 
    {
		
			
			Object obj = getObjectField(objeto, attributo);
	    	String atributo = getNameField(attributo);
	    	
	    	try {
				Field field = obj.getClass().getDeclaredField(atributo);
				field.setAccessible(true);
				field.set(obj, value);
				return obj;
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		
		
		return null;
    	
	}
	
	public static Object setValueAt2(Object objeto, Object value, String attributo) 
	{

		Object obj = null;
		String atributo = null;

		if(attributo.contains(".")){
			String attObj = attributo.substring(0, attributo.indexOf("."));
			String att = attributo.substring(attributo.indexOf(".")+1, attributo.length());
			
			try {
				Field f = objeto.getClass().getDeclaredField(attObj);
				f.setAccessible(true);
				Object v = f.get(objeto);
				if(v==null)
					v = f.getType().newInstance();
				if(value!=null){
					setValueAt2(v, value, att);
					atributo = attObj;
					obj = objeto;
					value = v;
				}
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
			
		}else{
			obj = getObjectField(objeto, attributo);
			atributo = getNameField(attributo);
		}

		

		try {
			Field field = obj.getClass().getDeclaredField(atributo);
			field.setAccessible(true);
			field.set(obj, value);
			return obj;
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}


		return null;

	}
	
	public static String getNameField (String field)
    {
    	if (field.contains(".")) //$NON-NLS-1$
    		return getNameField(field.substring(field.indexOf('.')+1));
    	return field;    	    		
    }
    
    public static Object getObjectField (Object obj, String field)
    {    
    	if (field.contains(".")) //$NON-NLS-1$
    	{
    		
    		Field[] fields = obj.getClass().getDeclaredFields();        	
        	try {
        		
    	    	for (int i = 0; i < fields.length; i++)
    	    	{
    	    		fields[i].setAccessible(true);
    	    		if (fields[i].getName().equals(field.substring(0, field.indexOf(".")))) //$NON-NLS-1$
    	    		{
    	    			Object obj2 = fields[i].get(obj); 
    	    			if (obj2 == null){
    	    				obj2 = fields[i].getType().newInstance();
    	    				fields[i].set(obj, obj2);
    	    			}
    	    			
    	    			if(obj2!=null)
    	    				return getObjectField(obj2, field.substring(field.indexOf('.')+1));    	    				
    	    			
    	    			return null;
    	    		}
    	    	}
        	} catch (IllegalArgumentException e) {
    			e.printStackTrace();
    		} catch (IllegalAccessException e) {
    			e.printStackTrace();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}    		
    	}
    		
    	return obj;
    }
    
    public static Object getValueField (Object obj, String field)
    {    	
    	if (obj == null)
    		return null; 
    	
    	Field[] fields = obj.getClass().getDeclaredFields();
    	
    	try {
    		
	    	for (int i = 0; i < fields.length; i++)
	    	{
	    		fields[i].setAccessible(true);
	    		if (fields[i].getName().equals(field))
	    			return fields[i].get(obj);	    			
	    	}
    	} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
}
